import urllib.request
import json
import re 

headers =  {'authority':'bbs.byr.cn',
			'method':'GET',
			'path':'/att/Beauty/321787/568',
			'scheme':'https',
			'accept':'image/webp,image/apng,image/*,*/*;q=0.8',
			'accept-encoding':'deflate, br',
			'accept-language':'zh-CN,zh;q=0.9',
			'cookie':'_ga=GA1.2.1468172914.1508769696; login-user=lyang1817; nforum-left=100; Hm_lvt_38b0e830a659ea9a05888b924f641842=1517741770,1519360014; left-index=0000000000; nforum[site]=yamb; nforum[XWJOKE]=hoho; Hm_lpvt_38b0e830a659ea9a05888b924f641842=1519380310; nforum[UTMPUSERID]=lyang1817; nforum[UTMPKEY]=8189477; nforum[UTMPNUM]=6788; nforum[PASSWORD]=AwtoZt69kfcsv7LJsEyMZQ%3D%3D',
			'if-modified-since':'Sun, 21 Jan 2018 11:27:52 GMT',
			'referer':'https://bbs.byr.cn/n/home/topten',
			'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Mobile Safari/537.36'
			}

response = urllib.request.Request('https://bbs.byr.cn/n/b/home/topten.json', headers=headers)
html = urllib.request.urlopen(response)  
result = html.read()
data=json.loads(result.decode('utf-8'))
with open("test.json","w") as f:
	json.dump(data,f)
	print("Success in printing data")

#json_str = json.dumps(data,ensure_ascii=False)
#print(data['data'][0]['id'])    
n = 0
board=[None]*10
id=[None]*10
jepitd=[None]*10   #JsonOfEachPageInTypeOfDict
JsonOfPages=[None]*10  
jep=[None]*10 #JsonOfEachPage
dataaa=[None]*10
q=0

while n < 10:
	hot=[]    
	content=[]  
	board[n]=data['data'][n]['board_name']
	#board[n] = re.findall('.(\w+).*', data['data'][n]['board_name'])
	id[n]=data['data'][n]['id'] 

	responsee = urllib.request.Request('https://bbs.byr.cn/n/b/article/'+board[n]+'/'+str(id[n])+'.json?page=1')
	htmll = urllib.request.urlopen(responsee)  
	resultt = htmll.read()
	dataa=json.loads(resultt.decode('utf-8'))  
	jep[n]=dataa
	jepitd[n]=dataa    
	pagination=dataa['data']['pagination']['total']  
	title=dataa['data']['title']
	j=0
	while j<len(dataa['data']['popularReplies']):
		hot.append(dataa['data']['popularReplies'][j])
		j=j+1                     
	k=0
	while k<len(dataa['data']['articles']):
		content.append(dataa['data']['articles'][k])
		k=k+1                     

	m=1
	while m< pagination :
		responsee = urllib.request.Request('https://bbs.byr.cn/n/b/article/'+board[n]+'/'+str(id[n])+'.json?page='+str((m+1)))
		htmll = urllib.request.urlopen(responsee)  
		resultt = htmll.read()
		dataa=json.loads(resultt.decode('utf-8'))  
		k=0
		while k<len(dataa['data']['articles']):
			content.append(dataa['data']['articles'][k])
			k=k+1                      
		m=m+1
	    
	JsonOfPages={
			"data": {
					"title":title,
					"hotreply":hot,
					"reply":content
					}   
				}	
	with open("test"+str(n)+".json","w") as f:
		json.dump(JsonOfPages,f)
		print("Success!")
	n=n+1                   

#print(jep)
#print (pagination)
#print(hot)
#f = open('test.txt', 'w')  
#f.write(json.dumps(JsonOfPages,ensure_ascii=False))
#f.close()
